package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list", name = "查询所有用户数据")
    public String fandAll(@RequestParam(defaultValue = "1", name = "page") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Role> page = roleService.findPage(getCompanyId(), pageNum, pageSize);
        request.setAttribute("page", page);
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toAdd", name = "进入到添加用户的页面")
    public String toAdd() {
        return "system/role/role-add";
    }

    @RequestMapping(value = "/edit", name = "保存用户数据")
    public String edit(Role role) {
        if (StringUtils.isEmpty(role.getId())) {
            role.setId(UUID.randomUUID().toString());
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            role.setCreateTime(new Date());
            roleService.save(role);
        } else {
            role.setUpdateTime(new Date());
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "进入修改用户页面")
    public String toUpdate(String id) {
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        return "system/role/role-add";
    }

    @RequestMapping(value = "/delete", name = "删除用户方法")
    public String delete(String id) {
        roleService.deleteById(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule", name = "进入角色分配权限页面")
    public String roleModule(String roleid) {
        Role role = roleService.findById(roleid);
        request.setAttribute("role", role);
        return "system/role/role-module";
    }

    @RequestMapping(value = "/getZtreeNodes", name = "构造模块数据")
    @ResponseBody
    public List<Map> getZtreeNodes(String roleid) {
        List<String> moduleIdList = moduleService.findModulesByRoleId(roleid);
        //1.查询所有的模块
        List<Module> moduleList = moduleService.findModuleListByCompanyId(getCompanyId());
        //2.构造map集合
        List<Map> list = new ArrayList<>();
        Map map = null;
        for (Module s : moduleList) {
            map = new HashMap();
            map.put("id", s.getId());
            map.put("pId", s.getParentId());
            map.put("name", s.getName());
            if (moduleIdList.contains(s.getId())) {
                map.put("checked", true);
            }
            list.add(map);
        }
        return list;
    }

    @RequestMapping(value="/updateRoleModule",name = "实现分配权限")
    public String updateRoleModule(String roleid,String moduleIds){
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }
}
